﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace BazookaBird.Sl.Data.Helpers
{
    public class TfsCredentials
    {
        public virtual string UserName { get; set; }
        
        [XmlIgnore]// Not be stored to Isolated Storage because of security
        public virtual string Password { get; set; }
        public virtual string CollectionPath { get; set; }
        public virtual string ProjectName { get; set; }
    }
    public static class IsolatedStorageHelper
    {
        public static void ClearCredentials(string fileName)
        {            
            using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Check if file exits
                if (isf.FileExists(fileName))
                {
                    isf.DeleteFile(fileName);
                }
            }
        }

        public static void SaveCredentials(TfsCredentials credentials, string fileName)
        {
            using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // If user choose to save, create a new file
                using (var fs = isf.CreateFile(fileName))
                {
                    // and serialize data
                    var serializer = new XmlSerializer(typeof(TfsCredentials));
                    serializer.Serialize(fs, credentials);
                }
            }
        }

        public static TfsCredentials ReadCredentials(string fileName)
        {
            // Try to load previously saved data from IsolatedStorage
            using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // check if file exits
                if (!isf.FileExists(fileName))
                {
                    // If previous data not found, create new instance
                    return new TfsCredentials();
                }
                
                using (var fs = isf.OpenFile(fileName, FileMode.Open))
                {
                    // Read the file contents and try to deserialize it back to data object
                    var ser = new XmlSerializer(typeof (TfsCredentials));
                    var obj = ser.Deserialize(fs) as TfsCredentials;

                    // If successfully deserialized, initialize data object variable with it
                    return obj ?? new TfsCredentials();
                }
            }
        }
    }
}
